<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class makerss
{
	private  $filename;
	private $CI;
	public $items = array(); //文章数组
	public $channel_title = "";
	public $channel_description = '';
	public $channel_link = '';
	public $language ='zh_cn';
	public $pubDate = '';
	public $lastBuildDate = '';
	public $generator ='';
	public $channel_imgurl = '';
	
	function __construct()
	{
	    $this->CI = &get_instance();
	    $this->CI->load->helper("url");
	    $this->CI->load->database();
	}
	
    public function item()
    {
            $art_array = $this->CI->db->select('*')->get("articles")->result_array();

            foreach($art_array as $art){
            	$this->items[] = $art;
            }
    }
    
    public function fetch()
    {
    	$rss = '<?xml version="1.0" encoding="utf-8" ?>'."\r\n";
    	$rss .= '<rss version="2.0">'."\r\n";
    	$rss .= "<channel>\r\n";
    	$rss .= "<title>{$this->channel_title}</title>\r\n";
    	$rss .= "<description><![CDATA[{$this->channel_description}]]></description>";
    	$rss .= "<link>{$this->channel_link}</link>\r\n";
        $rss .= "<language>{$this->language}</language>\r\n";    	
        
        if(!empty($this->pubDate)){
        	$rss .= "<pubDate>{$this->pubDate}</pubDate>\r\n";
        }
        if(!empty($this->lastBuildDate)){
        	$rss .= "<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>\r\n";
        }
        if(!empty($this->generator)){
        	$rss .= "<generator>{$this->generator}</generator>\r\n";
        	$rss .= "<tit>5</tit>\r\n";
        }
        if(!empty($this->channel_imgurl)){
            $rss .= "<image>\r\n";
            $rss .= "<title><![CDATA[{$this->channel_title}]]></title>\r\n";
            $rss .= "<link>{$this->channel_link}</link>\r\n";
            $rss .= "<url>{$this->channel_imgurl}</url>\r\n";
            $rss .= "</image>\r\n";
        }
        for($i=0;$i<count($this->items);$i++){
        	$rss .= "<item>\r\n";
        	$rss .= "<title>{$this->items[$i]['title']}</title>\r\n";
        	$rss .= "<link>".base_url('home/show').'/'.$this->items[$i]['id']."</link>\r\n";
        	$rss .= "<description><![CDATA[".substr(htmlspecialchars($this->items[$i]['body']),0,300)."]]></description>\r\n";
        	$rss .= "<pubDate>".date('Y,M,d H:m:s',$this->items[$i]['created'])."</pubDate>\r\n";
        	$rss .= "</item>\r\n";
        }
        $rss .= "</channel>\r\n</rss>\r\n";
        return $rss;
    
    }
    
    public function run()
    {
    	$this->item();
    	$rss = $this->fetch();
    	$fp = fopen('rss.xml','w');
    	fwrite($fp, $rss);
    	fclose($fp);
    }
	
	
}


















